package com.khalil.kkadmin.infrastructure.thread;


import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.concurrent.*;

@Slf4j
public class CustomisedTheadPool {

    public static ThreadPoolExecutor executor = new ThreadPoolExecutor(20, 100, 60 * 2000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(20), new ThreadFactory() {
        @Override
        public Thread newThread(@NotNull Runnable r) {
            return new Thread(r, "CustomisedTheadPool-" + executor.getPoolSize() + 1);
        }
    });

    public ThreadPoolExecutor getExecutor() {
        return executor;
    }

    public static void execute(Runnable runnable) {
        executor.execute(runnable);
        logInfo();
    }

    public static <T> Future<T> execute(Callable<T> callable) {
        Future<T> submit = executor.submit(callable);
        logInfo();
        return submit;
    }

    private static void logInfo() {
        log.info("当前线程数：{}，队列长度：{}", executor.getPoolSize(), executor.getQueue().size());
    }
}
